package 代码随想录.回溯.递增子序列;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class LeetCode491 {
    public List<List<Integer>> res = new ArrayList<>();
    public List<Integer> path = new ArrayList<>();

    public List<List<Integer>> findSubsequences(int[] nums) {
        backTracking(nums, 0);
        return res;
    }

    public void backTracking(int[] nums, int startIndex) {
        if (path.size() >= 2) {
            res.add(new ArrayList<>(path));
        }
        HashSet<Integer> set = new HashSet<>();
        for (int i = startIndex; i < nums.length; i++) {
            //同一树层值相同的元素不能取，不是递增的直接结束本次循环
            if (!path.isEmpty() && path.get(path.size() - 1) > nums[i] || set.contains(nums[i])) {
                continue;
            }
            set.add(nums[i]);
            path.add(nums[i]);
            backTracking(nums, i + 1);
            path.remove(path.size() - 1);//回溯
        }
    }

    public static void main(String[] args) {
        LeetCode491 code = new LeetCode491();
        int[] nums = {4, 6, 7, 7};
        System.out.println(code.findSubsequences(nums));
    }
}
